package com.ruoyi.system.util;


import cn.hutool.json.JSONUtil;
import org.apache.poi.xwpf.usermodel.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;


import java.io.IOException;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HtmlToWordUtil {

    // 将HTML转换为Word文档的核心方法
    public static XWPFDocument convertHtmlToWordDocument(String htmlContent, Map<String, String> variables) throws IOException {
        XWPFDocument document = new XWPFDocument();

        // 解析HTML
        Document jsoupDoc = Jsoup.parse(htmlContent);
        Element body = jsoupDoc.body();


        // TODO  测试
//        XWPFTable table = document.createTable(1, 2);
//        table.setWidth("100%");
//        XWPFTableRow row = table.getRow(0);
//        row.getCell(0).setWidth("50%");
//        row.getCell(1).setWidth("50%");
//
//        XWPFParagraph left = row.getCell(0).addParagraph();
//        left.setAlignment(ParagraphAlignment.LEFT);
//        XWPFRun leftRun = left.createRun();
//        leftRun.setText("cde");
//
//        XWPFParagraph regist = row.getCell(1).addParagraph();
//        regist.setAlignment(ParagraphAlignment.RIGHT);
//        XWPFRun registRun = regist.createRun();
//        registRun.setText("cde");
//        table.setTopBorder(XWPFTable.XWPFBorderType.NONE,0,0,"");
//table.setBottomBorder(XWPFTable.XWPFBorderType.NONE,0,0,"");
//table.setLeftBorder(XWPFTable.XWPFBorderType.NONE,0,0,"");
//table.setRightBorder(XWPFTable.XWPFBorderType.NONE,0,0,"");
//table.setInsideHBorder(XWPFTable.XWPFBorderType.NONE,0,0,"");
//table.setInsideVBorder(XWPFTable.XWPFBorderType.NONE,0,0,"");

        // 处理所有子元素
        for (Element element : body.children()) {
            processElement(element, document, variables);
        }

        return document;
    }
    // 将HTML转换为Word文档的核心方法
    public static XWPFDocument convertHtmlToWordDocument2(String htmlContent, Map<String, String> variables) throws IOException {
        XWPFDocument document = new XWPFDocument();

        // 解析HTML
        Document jsoupDoc = Jsoup.parse(htmlContent);
        Element body = jsoupDoc.body();

        //得到第一个h1标签
        Element h1 = body.getElementsByTag("h1").get(0);
        String style = h1.attr("style");
        //处理h1标签
//        processHeading(h1, document, 1, style);
        processHeading2(h1, document, 1, style);
        //得到span标签
        Element element0 = body.getElementsByTag("span").get(0);
        Element element1 = body.getElementsByTag("span").get(1);
        String style1 = element0.attr("style");
        //得到两个值
        String z1 = element0.text();
        String z2 = element1.text();
        //添加红色的直线
        addRedLine(document, z1, z2,style1);


        //处理div中的标签
        Element element4 = body.getElementsByTag("div").get(0);
        Elements p = element4.getAllElements();
        Elements div = body.select("div");
        for (Element item : div) {
            Element element = item.getElementsByTag("h2").get(0);
            //先创建h2标签
            XWPFParagraph paragraph = document.createParagraph();
            /**
             * 设置首行缩进 2
             */
            setFirstLineIndent(paragraph);
            XWPFRun run = paragraph.createRun();
            run.setText(element.text());
            run.setFontSize(16);
            CTR ctr = run.getCTR();
            CTRPr rPr =   ctr.isSetRPr()?ctr.getRPr(): ctr.addNewRPr();
            //设置中文字体
            CTFonts ctFonts  = rPr.isSetRFonts()?rPr.getRFonts():rPr.addNewRFonts();
            ctFonts.setEastAsia("黑体");


            //在创建p标签中的信息
            Elements p1 = item.getElementsByTag("p");
            // 处理所有子元素
            for (Element elementr : p1) {
                XWPFParagraph paragraphr = document.createParagraph();


                Elements spanList = elementr.select("span");
                /**
                 * 设置首行缩进 2
                 */
                setFirstLineIndent(paragraphr);
                Element element3 = spanList.get(0);
                XWPFRun run1 = paragraphr.createRun();
                run1.setText(element3.text());

                run1.setFontSize(16);
                run1.setFontFamily("楷体");


                Element element2 = spanList.get(1);
                XWPFRun runt = paragraphr.createRun();
                runt.setText(element2.text());
                runt.setFontSize(16);
                runt.setFontFamily("仿宋");

//            processElement(element, document, variables);

            }


        }


//        Element element2 = body.getElementsByTag("wz").get(0);
//        p.add(element2);
//        Element element3 = body.getElementsByTag("ww").get(0);
//        p.add(element3);

        // 处理所有子元素
//        for (Element element : p) {
//            XWPFParagraph paragraph = document.createParagraph();
//            Elements spanList = element.select("span");
//            Element element3 = spanList.get(0);
//            XWPFRun run1 = paragraph.createRun();
//            run1.setText(element3.text());
//            run1.setFontSize(16);
//            run1.setFontFamily("楷体");
//
//            Element element2 = spanList.get(1);
//            XWPFRun run = paragraph.createRun();
//            run.setText(element2.text());
//            run.setFontSize(16);
//            run.setFontFamily("仿宋");
//
////            processElement(element, document, variables);
//
//        }




        return document;
    }

    private static void setFirstLineIndent(XWPFParagraph paragraph) {

        CTPPr pPr = paragraph.getCTP().getPPr();
        if (pPr == null){
            pPr = paragraph.getCTP().addNewPPr();
        }
        CTInd ctInd = pPr.isSetInd() ? pPr.getInd(): pPr.addNewInd();

//       int fgg = 2*201;
//        String s = String.valueOf(fgg);
        ctInd.setFirstLine(BigInteger.valueOf(680));

    }
//    private static void setFirstLineIndentSpan(XWPFParagraph paragraph) {
//
//        CTPPr pPr = paragraph.getCTP().getPPr();
//        if (pPr == null){
//            pPr = paragraph.getCTP().addNewPPr();
//        }
//        CTInd ctInd = pPr.isSetInd() ? pPr.getInd(): pPr.addNewInd();
//
////       int fgg = 2*201;
////        String s = String.valueOf(fgg);
//        ctInd.setFirstLine(BigInteger.valueOf(420));
//
//
//    }

    public static XWPFDocument convertHtmlToWordYjbg(String htmlContent) throws IOException {
        XWPFDocument document = new XWPFDocument();

        // 解析HTML
        Document jsoupDoc = Jsoup.parse(htmlContent);
        Element body = jsoupDoc.body();

        //得到第一个h1标签
        Element h1 = body.getElementsByTag("h1").get(0);
        String style = h1.attr("style");
        //处理h1标签

        processHeading2(h1, document, 1, style,"");

        //得到span标签
        Element element0 = body.getElementsByTag("span").get(0);
        Element element1 = body.getElementsByTag("span").get(1);
        String style1 = element0.attr("style");
        //得到两个值
        String z1 = element0.text();
        String z2 = element1.text();
        //添加红色的直线
        addRedLine(document, z1, z2,style1);



        //处理div中的标签
        Element element4 = body.getElementsByTag("div").get(0);
        Elements p = element4.getAllElements();
        Elements div = body.select("div");
        for (Element item : div) {

//            h2Fuz(item,document);
//
//            h3uz(item,document);
//
            h2Bx(item,document);
        }





        return document;
    }

    private static void h2Bx(Element item, XWPFDocument document) {
        Elements elementList = item.getElementsByTag("wdc");

        for (Element elements : elementList){
            Element element = item.getElementsByTag("h2").get(0);
            //先创建h2标签
            XWPFParagraph paragraph = document.createParagraph();
            setFirstLineIndent(paragraph);
            XWPFRun run = paragraph.createRun();
            run.setText(element.text());
            run.setFontSize(16);
            CTR ctr = run.getCTR();
            CTRPr rPr =   ctr.isSetRPr()?ctr.getRPr(): ctr.addNewRPr();
            //设置中文字体
            CTFonts ctFonts  = rPr.isSetRFonts()?rPr.getRFonts():rPr.addNewRFonts();
            ctFonts.setEastAsia("楷体");


            pTj(elements,document);


        }


    }

    private static void h2Fuz(Element item,XWPFDocument document) {
        Element element = item.getElementsByTag("h2").get(0);
        //先创建h2标签
        XWPFParagraph paragraph = document.createParagraph();
        setFirstLineIndent(paragraph);
        XWPFRun run = paragraph.createRun();
        run.setText(element.text());
        run.setFontSize(16);
        CTR ctr = run.getCTR();
        CTRPr rPr =   ctr.isSetRPr()?ctr.getRPr(): ctr.addNewRPr();
        //设置中文字体
        CTFonts ctFonts  = rPr.isSetRFonts()?rPr.getRFonts():rPr.addNewRFonts();
        ctFonts.setEastAsia("楷体");
    }

    private static void h3uz(Element item,XWPFDocument document) {

        Elements elementList = item.getElementsByTag("wdc");
        for (Element elements : elementList){

            Element element = elements.getElementsByTag("h3").get(0);
            //先创建h2标签
            XWPFParagraph paragraph = document.createParagraph();
            setFirstLineIndent(paragraph);
            XWPFRun run = paragraph.createRun();
            run.setText(element.text());
            run.setFontSize(16);
            CTR ctr = run.getCTR();
            CTRPr rPr =   ctr.isSetRPr()?ctr.getRPr(): ctr.addNewRPr();
            //设置中文字体
            CTFonts ctFonts  = rPr.isSetRFonts()?rPr.getRFonts():rPr.addNewRFonts();
            ctFonts.setEastAsia("黑体");

            pTj(elements,document);


        }

    }

    private static void pTj(Element elements, XWPFDocument document) {
        XWPFParagraph paragraphr = document.createParagraph();
        setFirstLineIndent(paragraphr);
        Element element = elements.getElementsByTag("p").get(0);
        XWPFRun runt = paragraphr.createRun();
        runt.setText(element.text());
        runt.setFontSize(16);
        runt.setFontFamily("仿宋");
        //设置行间距为29
        setLineSpacing(paragraphr,29);
    }

    private static void setLineSpacing(XWPFParagraph paragraphr, int i) {
        CTP ctp = paragraphr.getCTP();
        CTPPr cTppr =   ctp.isSetPPr()?ctp.getPPr(): ctp.addNewPPr();
        CTSpacing spacing = cTppr.isSetSpacing()?cTppr.getSpacing():cTppr.addNewSpacing();

        spacing.setLine(BigInteger.valueOf((long)(i*20)));
        spacing.setLineRule(STLineSpacingRule.EXACT);

    }





    private static void addRedLine(XWPFDocument document,String z1,String z2,String style) {


        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun leftRun = paragraph.createRun();
        leftRun.setText(z1);
        //添加右边的文本
        for (int i = 0; i < 10; i++) {
            leftRun.addTab();
        }
        XWPFRun run = paragraph.createRun();
        run.setText(z2);
        paragraph.setAlignment(ParagraphAlignment.BOTH);
        if (style.contains("font-size:")) {
//            int fontSize = extractFontSize(style);
            run.setFontSize(16);
            run.setFontFamily("楷体");
//            int fontSize2 = extractFontSize(style);
            leftRun.setFontFamily("楷体");
            leftRun.setFontSize(16);
        }
        CTP ctp = paragraph.getCTP();
        CTBorder ctBorder = ctp.addNewPPr().addNewPBdr().addNewBottom();

        ctBorder.setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.Enum.forString("single"));
        ctBorder.setSz(BigInteger.valueOf(12));
        ctBorder.setSpace(BigInteger.valueOf(0));
        ctBorder.setColor("000000");

    }

    // 以下是处理HTML元素的辅助方法
    private static void processElement(Element element, XWPFDocument document, Map<String, String> variables) {
        String tagName = element.tagName().toLowerCase();
        String style = element.attr("style");

        switch (tagName) {
            case "h1":
                processHeading(element, document, 1, style);
                break;
            case "h2":
//                processHeading(element, document, 2, style);
                processH2(element, document, 2, style);

                break;
            case "h3":
                processHeading(element, document, 3, style);
                break;
            case "p":
                processParagraph(element, document, style, variables);
                break;

            case "table":
                processTable(element, document, style);
                break;
            case "ul":
            case "ol":
                processList(element, document, style, variables);
                break;
            case "li":
                // 在processList中处理
                break;
            case "tr":
            case "td":
                // 在processTable中处理
                break;
            case "span":
//                spanList(element,document,style);
                break;
            case "wz":
                line(element,document);
                break;
            case "ww":
                lineH4(element,document,style);
                break;
            default:
                // 其他标签作为普通段落处理
//                processParagraph(element, document, style, variables);
                break;
        }
    }

    private static void processH2(Element element, XWPFDocument document, int i, String style) {
        XWPFParagraph paragraph = document.createParagraph();
        setParagraphAlignment(paragraph, style);
        XWPFRun run = paragraph.createRun();
        run.setText(element.text());
        run.setFontSize(16);
        CTR ctr = run.getCTR();
        CTRPr rPr =   ctr.isSetRPr()?ctr.getRPr(): ctr.addNewRPr();
        //设置中文字体
        CTFonts ctFonts  = rPr.isSetRFonts()?rPr.getRFonts():rPr.addNewRFonts();
        ctFonts.setEastAsia("黑体");



    }

    private static void processElement2(Element element, XWPFDocument document, Map<String, String> variables) {
        String tagName = element.tagName().toLowerCase();
        String style = element.attr("style");

        switch (tagName) {
            case "h1":
                processHeading(element, document, 1, style);
                break;
            case "h2":
                processHeading(element, document, 2, style);
                break;
            case "h3":
                processHeading(element, document, 3, style);
                break;
            case "p":
                processParagraph(element, document, style, variables);
                break;

            case "table":
                processTable(element, document, style);
                break;
            case "ul":
            case "ol":
                processList(element, document, style, variables);
                break;
            case "li":
                // 在processList中处理
                break;
            case "tr":
            case "td":
                // 在processTable中处理
                break;
            case "span":
//                spanList(element,document,style);
                break;
            case "wz":
                line(element,document);
                break;
            case "ww":
                lineH4(element,document,style);
                break;
            default:
                // 其他标签作为普通段落处理
//                processParagraph(element, document, style, variables);
                break;
        }
    }
    private static void line(Element element, XWPFDocument document) {
        XWPFParagraph paragraph = document.createParagraph();
        CTP ctp = paragraph.getCTP();
        CTBorder ctBorder = ctp.addNewPPr().addNewPBdr().addNewTop();
        ctBorder.setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.Enum.forString("single"));
        ctBorder.setSz(BigInteger.valueOf(8));
        ctBorder.setSpace(BigInteger.valueOf(0));
        ctBorder.setColor("000000");

    }
    private static void lineH4(Element element, XWPFDocument document ,String style) {
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun leftRun = paragraph.createRun();
        leftRun.setText(element.text());
        leftRun.setFontFamily("仿宋");
        leftRun.setFontSize(14);

        CTP ctp = paragraph.getCTP();
        CTBorder ctBorder = ctp.addNewPPr().addNewPBdr().addNewBottom();
        ctBorder.setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.Enum.forString("single"));
        ctBorder.setSz(BigInteger.valueOf(8));
        ctBorder.setSpace(BigInteger.valueOf(0));
        ctBorder.setColor("000000");

    }

    private static void spanList(Element element, XWPFDocument document, String style) {

        XWPFParagraph paragraph = document.createParagraph();
        //添加左边的文本
        XWPFRun leftRun = paragraph.createRun();
        leftRun.setText(element.text());
        //添加右边的文本
        for (int i = 0; i < 15; i++) {
            leftRun.addTab();
        }
        XWPFRun run = paragraph.createRun();
        run.setText("2023");
        paragraph.setAlignment(ParagraphAlignment.BOTH);

    }

    private static void spanParage(String s1,String s2, XWPFDocument document, String style) {

        XWPFParagraph paragraph = document.createParagraph();
        //添加左边的文本
        XWPFRun leftRun = paragraph.createRun();
        leftRun.setText(s1);
        //添加右边的文本
        for (int i = 0; i < 10; i++) {
            leftRun.addTab();
        }
        XWPFRun run = paragraph.createRun();
        run.setText(s2);

        paragraph.setAlignment(ParagraphAlignment.BOTH);
        if (style.contains("font-size:")) {
            int fontSize = extractFontSize(style);
            run.setFontSize(fontSize);
            int fontSize2 = extractFontSize(style);
            leftRun.setFontSize(fontSize2);
        }

    }

    private static void spanParagraph(Element element, XWPFDocument document, String style) {
        XWPFParagraph paragraph = document.createParagraph();

        setParagraphAlignment(paragraph, style);
        XWPFRun run = paragraph.createRun();
        run.setText(element.text());

//        if (style.contains("font-size:")) {
//            int fontSize = extractFontSize(style);
//            run.setFontSize(fontSize);
//        }
//
//        if (style.contains("color:")) {
//            String color = extractColor(style);
//            String s = COLOR_MAP.get(color);
//
//            run.setColor(s);
//        }
    }

    private static void processHeading2(Element element, XWPFDocument document, int level, String style) {
        XWPFParagraph paragraph = document.createParagraph();
        setParagraphAlignment(paragraph, style);

        XWPFRun run = paragraph.createRun();
        run.setText(element.text());
        run.setFontSize(36);
        CTR ctr = run.getCTR();
        CTRPr rPr =   ctr.isSetRPr()?ctr.getRPr(): ctr.addNewRPr();
        //设置中文字体
        CTFonts ctFonts  = rPr.isSetRFonts()?rPr.getRFonts():rPr.addNewRFonts();
        ctFonts.setEastAsia("方正小标宋简体");


//        rPr.addNewSzCs().setVal(new BigInteger(String.valueOf(48)));
//        if (style.contains("font-size:")) {
////            int fontSize = extractFontSize(style);
//            run.setFontSize(72);
//        }


        if (style.contains("color:")) {
            String color = extractColor(style);
            String s = COLOR_MAP.get(color);

            run.setColor(s);
        }

        paragraph.setStyle("Heading" + level);
    }
    private static void processHeading2(Element element, XWPFDocument document, int level, String style,String zhanwei) {
        XWPFParagraph paragraph = document.createParagraph();
        setParagraphAlignment(paragraph, style);

        XWPFRun run = paragraph.createRun();
        run.setText(element.text());
        run.setFontSize(22);
        CTR ctr = run.getCTR();
        CTRPr rPr =   ctr.isSetRPr()?ctr.getRPr(): ctr.addNewRPr();
        //设置中文字体
        CTFonts ctFonts  = rPr.isSetRFonts()?rPr.getRFonts():rPr.addNewRFonts();
        ctFonts.setEastAsia("方正小标宋简体");


//        rPr.addNewSzCs().setVal(new BigInteger(String.valueOf(48)));
//        if (style.contains("font-size:")) {
////            int fontSize = extractFontSize(style);
//            run.setFontSize(72);
//        }


        if (style.contains("color:")) {
            String color = extractColor(style);
            String s = COLOR_MAP.get(color);

            run.setColor(s);
        }

        paragraph.setStyle("Heading" + level);
    }
    private static void processHeading(Element element, XWPFDocument document, int level, String style) {
        XWPFParagraph paragraph = document.createParagraph();
        setParagraphAlignment(paragraph, style);

        XWPFRun run = paragraph.createRun();
        run.setText(element.text());

        if (style.contains("font-size:")) {
//            int fontSize = extractFontSize(style);
            run.setFontSize(72);
        }

        if (style.contains("color:")) {
            String color = extractColor(style);
            String s = COLOR_MAP.get(color);

            run.setColor(s);
        }

        paragraph.setStyle("Heading" + level);
    }

    private static void processParagraph(Element element, XWPFDocument document, String style, Map<String, String> variables) {
        XWPFParagraph paragraph = document.createParagraph();
        setParagraphAlignment(paragraph, style);


        if (style.contains("border-bottom:") || style.contains("border-top:")) {
            setParagraphBorders(paragraph, style);
        }

        XWPFRun run = paragraph.createRun();

        String text = element.text();
        switch (text){
            case "一、军事事件":
                run.addTab();
                break;
            case "二、政治事件":
                run.addTab();
                break;
            case "三、外交事件":
                run.addTab();
                break;
            case "四、科技事件":
                run.addTab();
                break;
            case "承办单位：xxx":
                break;
            default:
                run.addTab();
                run.addTab();
        }

        // 替换变量
        if (text.startsWith("${") && text.endsWith("}")) {
            String varName = text.substring(2, text.length() - 1);
            text = variables.getOrDefault(varName, "");
        }

        run.setText(text);

        if (style.contains("font-size:")) {
            int fontSize = extractFontSize(style);
            run.setFontSize(fontSize);
        }

        if (style.contains("color:")) {
            String color = extractColor(style);

            run.setColor( COLOR_MAP.get(color));
        }
    }

    private static void processList(Element element, XWPFDocument document, String style, Map<String, String> variables) {
        boolean isOrdered = element.tagName().equalsIgnoreCase("ol");

        for (Element li : element.select("li")) {
            XWPFParagraph paragraph = document.createParagraph();
            setParagraphAlignment(paragraph, style);

            XWPFRun run = paragraph.createRun();
            String text = li.text();

            // 替换变量
            if (text.startsWith("${") && text.endsWith("}")) {
                String varName = text.substring(2, text.length() - 1);
                text = variables.getOrDefault(varName, "");
            }

            // 添加列表标记
            if (isOrdered) {
                // 有序列表使用数字
                int index = element.select("li").indexOf(li) + 1;
                run.setText(index + ". " + text);
            } else {
                // 无序列表使用圆点
                run.setText("• " + text);
            }

            if (style.contains("font-size:")) {
                int fontSize = extractFontSize(style);
                run.setFontSize(fontSize);
            }

            if (style.contains("color:")) {
                String color = extractColor(style);

                run.setColor( COLOR_MAP.get(color));
            }
        }
    }

    private static void processTable(Element element, XWPFDocument document, String style) {

        XWPFTable table = document.createTable();

        if (style.contains("width:")) {
            table.setWidth("100%");
        }

        Elements rows = element.select("tr");
        for (Element row : rows) {

            XWPFTableRow tableRow = table.createRow();


            Elements cells = row.select("td");
            for (int i = 0; i < cells.size(); i++) {
                Element cell = cells.get(i);
                XWPFTableCell tableCell = tableRow.getCell(row.children().indexOf(cell));

                if (tableCell ==null) tableCell = tableRow.addNewTableCell();
                tableCell.setWidth("100%");
                tableCell.setText(cell.text());

                CTTcPr tcPr = tableCell.getCTTc().getTcPr();
                if (tcPr == null){
                    tcPr = tableCell.getCTTc().addNewTcPr();
                }


                CTTcBorders borders =   tcPr.isSetTcBorders()?tcPr.getTcBorders(): tcPr.addNewTcBorders();

                CTBorder ctBorder = borders.addNewTop();
                ctBorder.setVal(STBorder.NONE);

                CTBorder left = borders.addNewLeft();
                left.setVal(STBorder.NONE);
                CTBorder right = borders.addNewRight();
                right.setVal(STBorder.NONE);

//                XWPFParagraph paragraph = tableCell.getParagraphs().get(0);
//                if (paragraph == null) {
//                    paragraph = tableCell.addParagraph();
//                }
//

                String cellStyle = cell.attr("style");

//                setParagraphAlignment(paragraph, cellStyle);
//
//                XWPFRun run = paragraph.createRun();
//                run.setText(cell.text());

                if (cellStyle.contains("font-size:")) {
                    int fontSize = extractFontSize(cellStyle);
                    XWPFRun run = tableCell.getParagraphs().get(0).createRun();
                    run.setFontSize(fontSize);
                }

                if (cellStyle.contains("text-align: right")) {
                    tableCell.getParagraphs().get(0).setAlignment(ParagraphAlignment.RIGHT);
                }

//                if (cellStyle.contains("background-color:")) {
//                    String bgColor = extractColor(cellStyle.replace("color:", "xxcolorxx:").replace("background-color:", "color:"));
//                    tableCell.setColor(COLOR_MAP.get(bgColor));
//                }
            }
        }
    }

    private static void setParagraphAlignment(XWPFParagraph paragraph, String style) {
        if (style.contains("text-align: center")) {
            paragraph.setAlignment(ParagraphAlignment.CENTER);
        } else if (style.contains("text-align: right")) {
            paragraph.setAlignment(ParagraphAlignment.RIGHT);
        } else if (style.contains("text-align: justify")) {
            paragraph.setAlignment(ParagraphAlignment.BOTH);
        }
    }

    private static void setParagraphBorders(XWPFParagraph paragraph, String style) {
        CTPPr pPr = paragraph.getCTP().getPPr();
        if (pPr == null) {
            pPr = paragraph.getCTP().addNewPPr();
        }

        if (style.contains("border-top:")) {
            CTBorder topBorder = pPr.addNewPBdr().addNewTop();
            String borderStyle = extractBorderStyle(style, "border-top");
            applyBorderStyle(topBorder, borderStyle);
        }

        if (style.contains("border-bottom:")) {
            CTBorder bottomBorder = pPr.addNewPBdr().addNewBottom();
            String borderStyle = extractBorderStyle(style, "border-bottom");
            applyBorderStyle(bottomBorder, borderStyle);
        }
    }

    private static String extractBorderStyle(String style, String borderType) {
        int start = style.indexOf(borderType + ":") + borderType.length() + 1;
        int end = style.indexOf(";", start);
        if (end == -1) {
            end = style.length();
        }
        return style.substring(start, end).trim();
    }

    private static void applyBorderStyle(CTBorder border, String borderStyle) {
        String[] parts = borderStyle.split(" ");
        if (parts.length >= 3) {
            border.setVal(STBorder.Enum.forString(parts[0]));
            border.setSz(BigInteger.valueOf(Long.parseLong(parts[0].replace("px", "")) * 8));
            String replace = parts[2].replace(";", "");
            String s = COLOR_MAP.get(replace);
            border.setColor(s);
        }
    }

    private static int extractFontSize(String style) {
        try {
            int start = style.indexOf("font-size:") + "font-size:".length();
            int end = style.indexOf("px", start);
            String sizeStr = style.substring(start, end).trim();
            return Integer.parseInt(sizeStr);
        } catch (Exception e) {
            return 12;
        }
    }

    private static final   Map<String,String> COLOR_MAP = new HashMap<>();
    static {
        COLOR_MAP.put("red","FF0000");
        COLOR_MAP.put("black","000000");
    }

    private static String extractColor(String style) {
        try {
            int start = style.indexOf("color:") + "color:".length();
            int end = style.indexOf(";", start);
            if (end == -1) {
                end = style.length();
            }
            return style.substring(start, end).trim().replace(";", "");
        } catch (Exception e) {
            return "000000";
        }
    }

}
